/**
 * @模块名：demo
 * @包名：com.example.xml
 * @描述：XmlUtil.java
 * @版本：1.0
 * @创建人：李凯
 * @创建时间：2020年12月28日下午6:52:09
 */
package com.example.xml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * @模块名：demo
 * @包名：com.example.xml
 * @类名称：XmlUtil
 * @类描述：
 * @版本：1.0
 * @创建人：李凯
 * @创建时间：2020年12月28日下午6:52:09
 */
public class XmlUtil {

	public static void main(String[] args) {
		String path = XmlUtil.class.getResource("").getPath() + "/1.txt";
		String xmlStr = readTxtFile(path, "GBK");
		Map<String, Object> map = build(xmlStr);
		System.out.println(map);
	}

	public static Map<String, Object> build(String xmlStr) {
		try {
			System.out.println("==" + xmlStr);
			Map<String, Object> map = new HashMap<String, Object>();
			Document doc = DocumentHelper.parseText(xmlStr);
			Element root = doc.getRootElement();
			xmlAnalys(root, map, "appBody");
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static void xmlAnalys(Element elem, Map<String, Object> map, String xmlList) {
		Iterator<?> iterator = elem.elementIterator();
		if (elem.getName().equals(xmlList)) {
			Map<String, Object> innermap = new HashMap<String, Object>();
			int i = 1;
			while (iterator.hasNext()) {
				Element field = (Element) iterator.next();
				String eleName = field.getName();
				String value = field.getText();
				Object obj = innermap.get(eleName);
				if (obj == null) {
					xmlAnalys(field, innermap, xmlList);
				} else {
					if (obj instanceof java.util.Map) {
						List<Object> list1 = new ArrayList<Object>();
						list1.add(innermap.remove(eleName));
						xmlAnalys(field, innermap, xmlList);
						list1.add(innermap.remove(eleName));
						innermap.put(eleName, list1);
					} else if (obj instanceof String) {
						innermap.put(eleName + i, value);
						i++;
					} else {
						xmlAnalys(field, innermap, xmlList);
						Map<String, Object> listValue = (Map<String, Object>) innermap.get(eleName);
						((List<Map<String, Object>>) obj).add(listValue);
						innermap.put(eleName, obj);
					}
				}
			}
			map.put(elem.getName(), innermap);
		} else {
			while (iterator.hasNext()) {
				xmlAnalys((Element) iterator.next(), map, xmlList);
			}
		}
	}

	public static String readTxtFile(String filePath, String charset) {
		String result = "";
		try {
			String encoding = charset;
			File file = new File(filePath);
			if (file.isFile() && file.exists()) {
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					result += lineTxt;
				}
				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return result;
	}
}
